let app = getApp()
var stylecss = require('../utils/stylecss.js');
Component({
    properties: {
      item: {
        type:Object
      }
      },
      data:{
        show: !0,
        audioAction: {
            method: "pause"
        },
        audio: null,
        time: 0,
        allTime: 0,
        status: 0,
        interval: null,
        initAudio: !0
      },

        detached: function () {
          this.data.audioCtx.stop()
          this.data.audioCtx.destroy()
        },
   
    ready: function() { 
      var that=this
      this.data.audioCtx.onCanplay(function() {
        that.data.audioCtx.duration;
        setTimeout(function() {
             that.setData({
               allTime:that.formatSecond(Math.ceil(that.data.audioCtx.duration))
             })
            },1000);
        })
    },
    lifetimes: {
        attached: function () {
            var css=stylecss.css(this.data.item.id);
            this.setData({
              css:css
            })
      var that=this
      this.data.audioCtx = wx.createInnerAudioContext()
      this.data.audioCtx.src = this.data.item.url
      this.data.audioCtx.obeyMuteSwitch = !1
      if(this.data.item.auto_play==1){
         this.data.audioCtx.autoplay = true
         that.setData({
          status:1
        })
      }else{
        this.data.audioCtx.autoplay = false
        that.setData({
          status:0
        })
      }
      this.data.audioCtx.onCanplay(function() {
       setTimeout(function() {
            that.setData({
              allTime:that.formatSecond(Math.ceil(that.data.audioCtx.duration))
            })
           }, 30);
       }),
       this.data.audioCtx.onTimeUpdate(function() {
        that.setData({
          allTime:that.formatSecond(Math.ceil(that.data.audioCtx.duration))
        })
      }),
      this.data.audioCtx.onPlay(function() {
        that.setData({
          status:1
        })
      })
      this.data.audioCtx.onPause(function() {
        that.setData({
          status:2
        })
       })
       this.data.audioCtx.onStop(function() {
        that.data.audioCtx.stop()
        that.data.audioCtx.destroy()
        that.setData({
          status:0
        })
       })
       this.data.audioCtx.onEnded(function() {
        that.data.audioCtx.destroy()
        that.setData({
          status:0
        })
       })
       this.interval = setInterval(function() {
        that.setData({
          time:that.formatSecond(Math.ceil(that.data.audioCtx.currentTime))
        }) 
       }, 1050);
       }
      },
    methods: {
      formatSecond: function(t) {
        var i = Math.floor(t / 60), n = Math.floor(t % 60);
        return "".concat(i < 10 ? "0" + i : i, ":").concat(n < 10 ? "0" + n : n);
      },
      play:function(){
           if(this.data.status==0||this.data.status==2){
             this.data.audioCtx.play()
             this.setData({
                 status:1
             })
           }else{
            this.data.audioCtx.pause()
            this.setData({
              status:2
            })
           }
      }
    }
});
